<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {
    //管理员列表
    public function index(){
        $res=M('user t1')->join('auth_group_access t2 on t1.id=t2.uid')->join('auth_group t3 on t2.group_id=t3.id ')->where("t1.isadmin='1'")->field('t1.*,t3.title')->select();
        $this->assign('admin',$res);
        // echo "<pre />";
        // var_dump(M());
        // exit();
        $this->display();
    }
    //管理员分组
    public function Adminrole()
    {
        $group=M('auth_group')->select();
        foreach ($group as $k => $v) {
            $group[$k]['rtitle']=M('auth_rule')->where(array('id'=>array('in',$v['rules'])))->field('GROUP_CONCAT(title) as gtitle')->find();
        }
        $this->assign('group',$group);
    	$this->display();
    }
    //管理员分类
    public function Admincate()
    {
    	$res=M('auth_category')->select();
    	$this->assign('cate',$res);
    	$this->display();
    }
    //管理员规则
    public function Adminrule()
    {
        $res=M('auth_rule t1')->join('auth_category t2 on t1.cid=t2.id')->field('t1.*,t2.cname')->select();
        $this->assign('rule',$res);
        $cate= M('auth_category')->field('id,cname')->select();
        $this->assign('cate',$cate);

    	$this->display();
    }
    //规则编辑
    public function rule_edit()
    {   
        $id=I('get.id');
        $res=M('auth_rule t1')->join('auth_category t2 on t1.cid=t2.id')->field('t1.*,t2.cname')->where("t1.id='{$id}'")->find();
        $this->assign('rule',$res);
        $cate= M('auth_category')->field('id,cname')->select();
        $this->assign('cate',$cate);
        $this->display();
    }
    //增加规则
    // public function rule()
    // {
    //     $data=I('post.');
    //     if ($cid=M('auth_rule')->save($data)) {
    //          $arr=array(
    //             'error'=>0,
    //             'info'=>'修改成功！',
    //             );
    //          echo json_encode($arr);
    //          exit();
    //      }else{
    //         $arr=array(
    //             'error'=>1,
    //             'info'=>'操作失败！',
    //             );
    //          echo json_encode($arr);
    //          exit();
    //      }
    // }
    //单条删除规则
    public function delrule()
    {
       $id=I('post.id');
       if (M('auth_rule')->where("id='{$id}'")->delete()) {
           $arr=array(
                'error'=>0,
                'info'=>'删除成功！',
                );
             echo json_encode($arr);
             exit();
       }else{
        $arr=array(
                'error'=>1,
                'info'=>'操作失败！',
                );
             echo json_encode($arr);
             exit();
       }
    }
    //批量删除规则
    public function ruledelall()
    {
        $num=rtrim($_POST['num'],',');
        if (empty($num)) {
            $arr=array(
                'error'=>2,
                'info'=>'请选择你要删除的数据！',
                );
             echo json_encode($arr);
             exit();
        }
        
        if (M('auth_rule')->where("id in($num)")->delete()) {
           $arr=array(
                'error'=>0,
                'info'=>'删除成功！',
                );
             echo json_encode($arr);
             exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败！',
                );
             echo json_encode($arr);
             exit();
        }
    }
    //增加规则
    public function addrule()
    {   
        $data['cid']=I('post.cname');
        $data['name']=I('post.rules');
        $data['title']=I('post.name');
        if($id=M('auth_rule')->add($data)){
            $res=M('auth_rule t1')->join('auth_category t2 on t1.cid=t2.id')->where("t1.id='$id'")->field('t2.cname')->find();

            $arr=array(
                'error'=>0,
                'info'=>'增加成功！',
                'cid'=>$id,
                'cname'=>$res['cname']
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'增加失败！',
                );
            echo json_encode($arr);
            exit();
        }
    }
    // 管理员信息编辑
    public function adminedit()
    {   
        $id=I('get.id');
        $res=M('user t1')->join('auth_group_access t2 on t1.id=t2.uid')->join('auth_group t3 on t2.group_id=t3.id')->where(array('t1.id'=>$id))->field('t1.nickname,t1.email,t3.title')->find();
        $this->assign('info',$res);

        $res = M('auth_group')->select();
        $this->assign('title',$res);
        $this->display();
    }
    // 信息保存
    public function editsava()
    {
        $group_id=I('post.role');
        $uid=I('get.id');

        $res = M('auth_group_access')->where(array('uid'=>$uid))->setField('group_id',$group_id);
        if ($res) {
            $arr=array(
                'error'=>0,
                'info'=>'修改成功！',
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败！',
                );
            echo json_encode($arr);
            exit();
        }
    }
    // public function adminadd()
    // {
    //     $res = M('auth_group')->select();
    //     $this->assign('title',$res);
    //     $this->display();
    // }
    //分类编辑
    public function cate_edit()
    {   
        $id=I('get.id');
        $res= M('auth_category')->where(array('id'=>$id))->find();
        $this->assign('cate',$res);
        $this->display();
    }
    // 分类编辑提交
    public function catesave()
    {
        $id=I('get.id');
        $cname=I('post.cname');
        $res = M('auth_category')->where(array('id'=>$id))->setField('cname',$cname);
        if ($res) {
            $arr=array(
                'error'=>0,
                'info'=>'修改成功！',
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败！',
                );
            echo json_encode($arr);
            exit();
        }

    }
    //单条删除分类
    public function cate_del()
    {
       $id=I('post.uid');
       if(M('auth_category')->where(array('id'=>$id))->delete()){
            $arr=array(
                'error'=>0,
                'info'=>'删除成功！',
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败！',
                );
            echo json_encode($arr);
            exit();
        }
       var_dump(M());
    }
    //批量删除分类
    public function cate_delall()
    {
        $num=I('post.num');
        $num=rtrim($num,',');
        $res=M('auth_category')->where("id in($num)")->delete();
        if ($res) {
            $arr=array(
                'error'=>0,
                'info'=>'删除成功'
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败'
                );
            echo json_encode($arr);
            exit();
        }
    }
        //增加分类
    public function cateadd()
    {

        if (empty(I('post.cname'))){
            $arr=array(
                'error'=>2,
                'info'=>'操作失败,请输入分类名'
                );
            echo json_encode($arr);
            exit();
        }

        $data['cname']=I('post.cname');
        if ($id=M('auth_category')->add($data)) {
            $arr=array(
                'error'=>0,
                'info'=>'增加成功！',
                'id'=>$id
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败！'
                );
            echo json_encode($arr);
            exit();
        }
    }

    // 角色编辑页
    public function role_edit()
    {
        
        $res = M('auth_category t1')->join('auth_rule t2 on t1.id=t2.cid')->field('t1.cname,t1.id as cid,t2.title,t2.id as rid')->select();
        $arr=array();
        foreach ($res as $v) {
            $arr[$v['cname']][]=$v;
        }
        $this->assign('cate',$arr);
        $id=I('get.id');
        $res = M('auth_group')->where(array('id'=>$id))->find();
        $this->assign('info',$res);
        $rules = explode(',', $res['rules']);
        $this->assign('rule',$rules);

        $this->display();
    }

    public function role_save()
    {
        $id=I('post.id');
        $data['title']=I('post.title');
        $data['rules']=I('post.num');
        $data['desc']=I('post.desc');
        if(M('auth_group')->where(array('id'=>$id))->save($data)){
            $arr=array(
                'error'=>0,
                'info'=>'修改成功！',
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败,请操作要修改的数据'
                );
            echo json_encode($arr);
            exit();
        }
    }

    // 角色增加页
    public function rolepage()
    {
        $res = M('auth_category t1')->join('auth_rule t2 on t1.id=t2.cid')->field('t1.cname,t1.id as cid,t2.title,t2.id as rid')->select();
        $arr=array();
        foreach ($res as $v) {
            $arr[$v['cname']][]=$v;
        }
        $this->assign('cate',$arr);
        $this->display();
    }
    // 增加角色
    public function roleadd()
    {
        $num=I('post.num');
        $data['rules']=rtrim($num,',');
        $data['name']=I('post.name');
        $data['desc']=I('post.desc');
        if(M('auth_group')->add($data)){
            $arr=array(
                'error'=>0,
                'info'=>'增加成功！',
                'id'=>$id
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'操作失败！'
                );
            echo json_encode($arr);
            exit();
        }

    }

    //角色删除
    public function role_del()
    {
        
    }

    // 批量删除角色
    public function role_delall()
    {
        
    }
}
?>